package com.ptszyxx.popose.module.main.home.adapter;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.HomeTabEnum;
import com.ptszyxx.popose.databinding.ItemHomeBinding;
import com.ptszyxx.popose.module.main.home.vm.HomeTabVM;
import com.ptszyxx.popose.module.main.home.vm.HomeVM;
import com.ysg.http.data.entity.home.HomeResult;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

public class HomeAdapter extends BaseQuickAdapter<HomeResult, BaseDataBindingHolder<ItemHomeBinding>> {

    private HomeTabVM viewModel;
    private Presenter mPresenter = new Presenter();
    private String separator = " | ";
    private HomeTabEnum homeTabEnum;


    public HomeAdapter(HomeTabVM viewModel, HomeTabEnum homeTabEnum) {
        super(R.layout.item_home);
        this.viewModel = viewModel;
        this.homeTabEnum = homeTabEnum;
    }

    @Override
    protected void convert(BaseDataBindingHolder<ItemHomeBinding> holder, HomeResult item) {
        ItemHomeBinding binding = holder.getDataBinding();
        if (binding != null) {
            binding.setEntity(item);
            binding.setViewModel(viewModel);
            binding.setPresenter(mPresenter);
            binding.setPosition(holder.getBindingAdapterPosition());
            binding.executePendingBindings();

            //优先级：语音-》签名-》个人信息
            if (YStringUtil.isNotEmpty(item.getVoiceSignature())) {
                binding.voiceView.setData(item.getVoiceSignature(), item.getVoiceSignatureSeconds());
                binding.voiceView.setVisibility(View.VISIBLE);
                binding.tvSignature.setVisibility(View.GONE);
            } else if (YStringUtil.isNotEmpty(item.getQianming())){
                binding.tvSignature.setText(item.getQianming());
                binding.voiceView.setVisibility(View.GONE);
                binding.tvSignature.setVisibility(View.VISIBLE);
            }

            //个人信息：拼接
            StringBuffer sb = new StringBuffer();
            if (YStringUtil.isNotEmpty(item.getAddress())){
                sb.append(item.getAddress() + separator);
            }
            if (YStringUtil.isNotEmpty(item.getAge() + "") && YStringUtil.noEq(0, item.getAge() + "")){
                sb.append(item.getAge() + getContext().getResources().getString(R.string.year_old) + separator);
            }
            if (YStringUtil.isNotEmpty(item.getShengao())){
                sb.append(item.getShengao() + separator);
            }
            if (YStringUtil.isNotEmpty(item.getZhiyename())){
                sb.append(item.getZhiyename() + separator);
            }
            binding.tvInfo.setText(YStringUtil.clearEnd(sb.toString(), separator));

            if (HomeTabEnum.account == homeTabEnum){
                binding.ivSayHello.setVisibility(View.GONE);
                if (YStringUtil.eq(YSPUtils.getInstance().getUserPhone(), item.getLoginname())){
                    binding.tvCurrentAccount.setVisibility(View.VISIBLE);
                }
            }

        }
    }

    public static class Presenter{
        public void onItemClick(HomeTabVM viewModel, HomeResult entity){
            viewModel.jumpPersonDetail(entity);
        }
        public void onChatClick(HomeTabVM viewModel, HomeResult entity){
            viewModel.jumpChat(entity);
        }
    }







}
